package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.core.utils.StringUtils;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.mapper.BrandMapper;
import com.spzx.product.service.IBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 分类品牌 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-04-29
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements IBrandService {

    @Override
    public List<Brand> selectBrandList(String name) {
        LambdaQueryWrapper<Brand> queryWrapper = new LambdaQueryWrapper();//不考虑性能用like
        queryWrapper.like(StringUtils.isNoneBlank(name),Brand::getName,name);//有名字才会继续往下面查
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 新增
     * @param brand
     */
    @Override
    public void add(Brand brand) {
        //1.封装品牌创建者id
        brand.setCreateBy(SecurityUtils.getUsername());
        //2.根据品牌名称查询品牌是否存在
        long count = count(new LambdaQueryWrapper<Brand>().eq(Brand::getName, brand.getName()));
        if (count > 0) {
            throw new ServiceException("品牌名成已经存在");
        }
        //保存品牌
        save(brand);
    }
}
